package com.rong.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import com.opensymphony.xwork2.ActionSupport;
import com.rong.DAO.ZynPrice1DAO;
import com.rong.DAO.ZynPrice2DAO;
import com.rong.DAO.ZynPrice34DAO;
import com.rong.ORM.ZynPrice1;
import com.rong.ORM.ZynPrice2;
import com.rong.ORM.ZynPrice34;
import com.rong.hibernate.BaseHibernateDAO;

public class HeatWaterPriAct extends ActionSupport {

	public int comId=-1;																		
	public int carId=-1;																	
	public String isheat="";
	public String result="";
	/**
	 * @return the comId
	 */
	public int getComId() {
		return comId;
	}
	/**
	 * @param comId the comId to set
	 */
	public void setComId(int comId) {
		this.comId = comId;
	}
	/**
	 * @return the carId
	 */
	public int getCarId() {
		return carId;
	}
	/**
	 * @param carId the carId to set
	 */
	public void setCarId(int carId) {
		this.carId = carId;
	}
	/**
	 * @return the isheat
	 */
	public String getIsheat() {
		return isheat;
	}
	/**
	 * @param isheat the isheat to set
	 */
	public void setIsheat(String isheat) {
		this.isheat = isheat;
	}
	/**
	 * @return the result
	 */
	public String getResult() {
		return result;
	}
	/**
	 * @param result the result to set
	 */
	public void setResult(String result) {
		this.result = result;
	}
	
	
	/**
	 * 
	 * comId: 采热地点
	 * carID：车辆ID
	 * isheat:采热还是采水
	 * 
	 */

	BaseHibernateDAO b=new BaseHibernateDAO();
	
	 @Override
	public String execute()
	    throws ServletException, IOException {
		 
		 ZynPrice1DAO d=new ZynPrice1DAO();
		 ZynPrice2DAO d2=new ZynPrice2DAO();
	        HttpServletResponse response = ServletActionContext.getResponse()   ;
            PrintWriter out=response.getWriter();         
	        Session s=null;
	        try {
	        s=b.getSession();
		 	       
	        if(this.getIsheat().equalsIgnoreCase("y")){  //heat price1
		 	        	
		 	        	 Criteria c=s.createCriteria(ZynPrice1.class);
			 	         c.add(Restrictions.eq("comId", this.getComId()));
			 	         c.add(Restrictions.eq("carId", this.getCarId()));
			 	         c.add(Restrictions.or(Restrictions.isNull("invalidationDate"),Restrictions.gt("invalidationDate", new Date()))); //日期为空或大于当下日期
					        	
			 	         List<ZynPrice1> list=c.list();			        	
					        	if(list.size()>0){
							        	ZynPrice1 p1=(ZynPrice1)list.get(0);			        
							        	this.setResult(p1.getId()+"p"+p1.getPrice());
					        	}else{//没有对应记录，返回0		        		
					        			this.setResult("-1p0");
					        	}
		  
		      
		 	         }else{											// water price2
		 	        	 Criteria c2=s.createCriteria(ZynPrice2.class);
			 	         c2.add(Restrictions.eq("comId", this.getComId()));
			 	        // c2.add(Restrictions.eq("carId", this.getCarId()));
			 	         c2.add(Restrictions.or(Restrictions.isNull("invalidationDate"),Restrictions.gt("invalidationDate", new Date()))); //日期为空或大于当下日期
			 	       
				 	        	List<ZynPrice2> list2=c2.list();			        	
					        	if(list2.size()>0){
							        	ZynPrice2 p2=(ZynPrice2)list2.get(0);			        
							        	this.setResult(p2.getId()+"p"+p2.getPrice());
					        	}else{//没有对应记录，返回0		        		
					        			this.setResult("-1p0");
					        	}	 	        	 
		 	         }
		 	         
           
            
	        }
	        catch(Exception pe) {
	        	 out.println("-2p0");   
	             out.close();
	        	System.out.println("[Compute Action]..erro.]");
	        	System.out.println("ParseException: " + pe.toString());
	        }finally{
	        	 out.println(this.result);  
	             out.close();
	        	 return "SUCCESS";
	        }
	      
	    }
}
